<?php
namespace app\shop\model;
use app\BaseModel;
use think\facade\Db;



class BargainRecord extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_shop_bargain_record';


    protected $append = [

        'goods_status',

        'atv_status'

    ];


    /**
     * @author chenniang
     * @DataTime: 2021-01-22 14:14
     * @功能说明:活动状态 主要防止成功没有改状态
     */
    public function getStatusAttr($value,$data){

        if(isset($data['status'])&&isset($data['over_time'])&&isset($data['have_num'])&&isset($data['user_num'])&&!empty($data['id'])){

            $status = $data['status'];
            //成功
            if($status==1&&$data['have_num']>=$data['user_num']){

                $status = 2;

                $this->dataUpdate(['id'=>$data['id']],['status'=>$status]);

            }
            //失败
            if($status==1&&$data['over_time']<time()){

                $status = -1;

                $this->dataUpdate(['id'=>$data['id']],['status'=>$status]);
            }

            return $status;

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-21 15:59
     * @功能说明:活动状态
     */
    public function getAtvStatusAttr($value,$data){

        if(!empty($data['atv_id'])){

            $atv_model = new BargainAtv();

            $atv = $atv_model->dataInfo(['id'=>$data['atv_id']]);

            if($atv['status']==0||$atv['status']==-1){

                return 0;
            }

            if($atv['end_time']<time()){

                return 0;
            }

            return 1;

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-21 15:14
     * @功能说明:商品状态
     */
    public function getGoodsStatusAttr($value,$data){

        if(!empty($data['atv_id'])&&!empty($data['goods_id'])&&!empty($data['spe_id'])){

            $goods_model = new IndexGoods();

            $goods_status = $goods_model->where(['id'=>$data['goods_id']])->value('status');
            //商品下架
            if($goods_status==0){

                return 2;
            }
            //商品删除
            if($goods_status==-1){

                return 4;

            }
            //无库存
            $stock = $this->atvStockCheck($data['atv_id'],$data['spe_id']);

            if($stock==0){

                return 3;
            }

            return 1;

        }

    }


//    /**
//     * @author chenniang
//     * @DataTime: 2021-01-21 11:31
//     * @功能说明:原价
//     *
//     */
//    public function getInitPriceAttr($value,$data){
//
//        if(!empty($data['spe_id'])){
//
//            $spe_model = new AdminShopSpePrice();
//
//            $price = $spe_model->where(['id'=>$data['spe_id']])->value('price');
//
//            return $price;
//        }
//    }
//
//
//    /**
//     * @author chenniang
//     * @DataTime: 2021-01-21 11:16
//     * @功能说明:规格名
//     */
//    public function getSpeNameAttr($value,$data){
//
//        if(!empty($data['spe_id'])){
//
//            $spe_model = new AdminShopSpePrice();
//
//            $spe = $spe_model->dataInfo(['id'=>$data['spe_id']]);
//
//            $name= $spe['spe_name_text'];
//
//            return $name;
//        }
//
//    }

    /**
     * @author chenniang
     * @DataTime: 2020-12-09 14:17
     * @功能说明:列表
     */
    public function dataList($dis,$page){

       $data = $this->alias('a')
                ->join('longbing_card_user b','a.user_id = b.id','left')
                ->where($dis)
                ->field('a.*,b.nickName,b.avatarUrl')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

       if($data['data']){

           foreach ($data['data'] as &$v){

               $v['avatarUrl'] = !empty($v['avatarUrl'])?$v['avatarUrl']:'https://lbqnyv2.migugu.com/defaultAvatar.png';

           }

       }

       return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-09 14:18
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-19 10:36
     * @功能说明:编辑
     */
   public function dataUpdate($dis,$data){


       $res = $this->where($dis)->update($data);
       
       return $res;

   }

    /**
     * @author chenniang
     * @DataTime: 2021-01-19 10:46
     * @功能说明:详情
     */
    public function dataInfo($dis,$file= '*'){

        $data = $this->where($dis)->field($file)->find();

        return !empty($data)?$data->toArray():[];


    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-21 10:13
     * @功能说明:弹幕
     */
    public function dmList($atv_id){

        $dis = [

            'a.atv_id' => $atv_id,
        ];

        $data = $this->alias('a')
                ->join('longbing_card_user b','a.user_id = b.id')
                ->where($dis)
                ->where('a.status','>',1)
                ->field(['b.nickName','b.avatarUrl','a.success_time'])
                ->group('a.id')
                ->order('a.id desc')
                ->limit(5)
                ->select()
                ->toArray();

        if(!empty($data)){

            foreach ($data as &$v){

                $v['success_time'] = lb_friendly_date($v['success_time']);

                $v['avatarUrl'] = !empty($v['avatarUrl'])?$v['avatarUrl']:'https://lbqnyv2.migugu.com/defaultAvatar.png';
            }
        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-21 11:07
     * @功能说明:用户记录列表
     */
    public function userRecordList($dis){

        $data = $this->where($dis)->order('id desc')->paginate(10)->toArray();

        return $data;

    }



    /**
     * @author chenniang
     * @DataTime: 2021-01-21 14:55
     * @功能说明:检查活动库存
     */
    public function atvStockCheck($atv_id,$spe_id){

       $spe_model = new BargainAtvSpe();

       $dis = [

           'atv_id' => $atv_id,

           'spe_id' => $spe_id
       ];

       $spe = $spe_model->dataInfo($dis);

       if(empty($spe)||$spe['true_stock']<=0){

           return 0;
       }

        return 1;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-21 16:16
     * @功能说明:检查能不能砍价
     */
    public function isBarGain($record,$user_id,$atv_info){

        $dis = [

            'record_id' => $record['id'],

            'user_id'   => $user_id
        ];

        $list_model = new BargainRecordList();
        //是否看过改次活动
        $is = $list_model->where($dis)->find();

        if(!empty($is)){

            return 0;
        }

        $dis = [

            'atv_id' => $record['atv_id'],

            'user_id'=> $user_id
        ];
        //总的砍价次数
        $count = $list_model->where($dis)->count();

        if($count>=$atv_info['atv_times']){

            return 0;
        }

        return 1;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-02-01 16:43
     * @功能说明:砍价活动
     */
    public function DoAtv($input){

        if(!empty($input['bargain_id'])&&empty($input['collage_id'])&&empty($input['is_integral'])){

            $bargain_model = new BargainRecord();

            $bar_check = $bargain_model->checkOrder($input['order_id'],$input['bargain_id']);
            //抛出错误信息 并回滚
            if(!empty($bar_check['code'])){

                return $bar_check;

            }
            //砍价记录详情
            $bar_record  = $bargain_model->dataInfo(['id'=>$input['bargain_id']]);

            $bar_record['price'] = $bar_record['low_price'];

            return $bar_record;
        }

        return [];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-22 09:30
     * @功能说明:检查砍价订单并处理
     */
    public function checkOrder($order_id,$record_id){

        if(!empty($record_id)){
            //详情
            $record = $this->dataInfo(['id'=>$record_id]);
            //检查是不是砍价成功了
            if($record['status']!=2){

                return ['code'=>500,'msg'=>'砍价活动状态错误'];
            }

            if(!empty($record['order_id'])){

                return ['code'=>500,'msg'=>'该砍价活动里您有一笔待支付订单，请在“个人中心-订单管理”查看'];

            }
            //检查砍价库存
            $res = $this->atvStockCheck($record['atv_id'],$record['spe_id']);

            if($res!=1){

                return ['code'=>500,'msg'=>'砍价库存不足'];

            }

            $res = $this->dataUpdate(['id'=>$record_id],['order_id'=>$order_id]);

            if($res!=1){

                return ['code'=>500,'msg'=>'下单失败'];

            }


        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-22 15:51
     * @功能说明:我正在进行中的活动
     */
    public function myRecordIng($goods_id,$user_id){

        $dis = [

            'user_id' => $user_id,

            'goods_id'=> $goods_id
        ];
        //已经发起砍价的次数
        $id = $this->where($dis)->where('status','in',[1,2])->find();

        return $id;
    }



    /**
     * @author chenniang
     * @DataTime: 2022-01-21 14:59
     * @功能说明:砍价信息
     *
     */
    public function DoAtvPrice($input,$spe,$goods){
        //砍价记录详情
        $bar_record = $this->dataInfo(['id'=>$input['bargain_id']]);

        $spe['price']         = $bar_record['low_price'];

        $spe['spe_name_text'] = $bar_record['spe_name'];

        $goods['name']        = $bar_record['goods_name'];

        $goods['cover']       = $bar_record['goods_cover'];

        $data['spe']   = $spe;

        $data['goods'] = $goods;

        return $data;

    }











}